<?php

if (!defined('IN_ADMIN')) {
    exit('Access Denied');
}

$mpurl = 'admin.php?ac=diary';
$list = array();
if ($_GET ['op'] == "detail") {
    // 日记详情
    $id = intval($_GET ['id']);
} elseif ($_GET['op'] == "removeSquare") {
    $id 	= intval($_GET ['id']);
    $uid 	= intval($_GET ['uid']);
    $bodyId = intval($_GET['bodyId']);
    $label  = intval($_GET['label']);
    $param  = array(
        'do' 		=> 'postManage',
        'format'	=> 'json',
        'uid' 		=> $uid,
        'id' 		=> $id,
    	'bodyId' 	=> $bodyId,
    	'label'		=> $label,
    );
    $content = get_api_content($_SC ['apiurl'], $param);
    $content = json_decode($content);
    if ($content->resultData == 1) {
        $msg = "操作成功!";
        cpmessage($msg);
    }
    $msg = "操作失败，请重试一次!";
    if ($content->errorNo) {
        $msg .= "errorNo:" . $content->errorNo;
    }
    cpmessage($msg);
} elseif ($_GET['op'] == 'getDiaryAllInfo'){
    $bodyId = intval($_GET ['bodyId']);
    $uid    = intval($_GET ['uid']);
    $param  = array(
            'do' => 'getDiaryAllInfo',
            'format' => 'json',
            'uid' 	 => $uid,
            'bodyId' => $bodyId,
    );
    $content = get_api_content($_SC ['apiurl'], $param);
    $content = json_decode($content);
    $list = object_to_array($content->resultData);
    include_once template("admin/".'diary_info');
    exit;
}elseif ($_GET['op'] == 'removeComment'){
    
    $id  = intval($_GET['id']);	
    $uid = intval($_GET ['uid']);
    $param = array(
            'do' => 'removeComment',
            'format' => 'json',
            'uid' => $uid,
            'id' => $id,
    );
    $content = get_api_content($_SC ['apiurl'], $param);
    $content = json_decode($content);
    if ($content->resultData == 1) {
        $msg = "操作成功!";
        cpmessage($msg);
    }
    $msg = "操作失败，请重试一次!";
    if ($content->errorNo) {
        $msg .= "errorNo:" . $content->errorNo;
    }
    cpmessage($msg);
    
}elseif ($_GET['op'] == 'removeManyComment') {
    
    $bodyId  = intval($_GET['bodyId']);
    $uid     = intval($_GET ['uid']);
    $cUid    = intval($_GET['cUid']);
    $param = array(
            'do' => 'removeManyComment',
            'format' => 'json',
            'uid' => $uid,
            'bodyId' => $bodyId,
            'cUid' => $cUid,
    );
    $content = get_api_content($_SC ['apiurl'], $param);
    $content = json_decode($content);
    if ($content->resultData == 1) {
        $msg = "操作成功!";
        cpmessage($msg);
    }
    $msg = "操作失败，请重试一次!";
    if ($content->errorNo) {
        $msg .= "errorNo:" . $content->errorNo;
    }
    cpmessage($msg);
    
}elseif ($_GET['op'] == 'getCommentInfoList'){

    $start = intval($_GET ['start']);
    $perpage = intval($_GET ['perpage']) > 0 ? intval($_GET ['perpage']) : 20;
    $uid  = intval($_GET['uid']);
    $type = intval($_GET['type']);
    $bodyId = intval($_GET['bodyId']);
    
    $param = array(
            'do' => 'getCommentInfoList',
            'format' => 'json',
            'start' => $start,
            'length' => $perpage,
            'uid' => $uid,
            'type' => $type,
            'bodyId' => $bodyId,
    );
    // 读取api数据
    $content = get_api_content($_SC ['apiurl'], $param);
    $content = json_decode($content);
    $list = object_to_array($content->resultData);
    
    // page
    $mpurl .='&uid='.$uid.'&type='.$type.'&bodyId='.$bodyId;
    $pageurl = multi($start, $perpage, $mpurl);
    $nextpurl = $pageurl['next'];
    $prevpurl = $pageurl['prev'];
    $mpurl = $pageurl['urls'];
    $firstpurl = $pageurl['first'];
    
    $perpages = array(
            $perpage => ' selected'
    );
    
    include_once template("admin/".'diaryCommenList');
    exit;
}elseif ($_GET['op'] == 'searchDiary'){
    $start    = intval($_GET ['start']);
    $perpage  = intval($_GET ['perpage']) > 0 ? intval($_GET ['perpage']) : 20;
    $uid  	  = intval($_GET['uid']);
    $title    = strval($_GET['title']);
    $nickname = strval($_GET['nickname']);
    
    $param = array(
            'do' 	 	=> 'fetchDiaryInfo',
            'format' 	=> 'json',
            'start' 	=> $start,
            'length' 	=> $perpage,
            'uid' 		=> $uid,
    );
    if (!empty($title))
    {
    	$param['title']	=	'%'.$title.'%';
    }else{
    	$param['title']	=	$title;
    }
    
    if (!empty($nickname))
    {
    	$param['nickname']	=	'%'.$nickname.'%';
    }else{
    	$param['nickname']	=	$nickname;
    }
    
    // 读取api数据
    $content = get_api_content($_SC ['apiurl'], $param);
    $content = json_decode($content);
    $list = object_to_array($content->resultData);
    
    // page
    $mpurl .='&uid='.$uid.'&nickname='.$nickname.'&title='.$title.'&start='.$start.'&length='.$perpage.'&op=searchDiary';
    $pageurl = multi($start, $perpage, $mpurl);
    $nextpurl = $pageurl['next'];
    $prevpurl = $pageurl['prev'];
    $mpurl = $pageurl['urls'];
    $firstpurl = $pageurl['first'];
    
    $perpages = array(
            $perpage => ' selected'
    );   
}else {
	
    // 列表
    $start = intval($_GET ['start']);
    // 参数过滤解析
    $perpage = intval($_GET ['perpage']) > 0 ? intval($_GET ['perpage']) : 20;
    
    // 搜索条件
    $intkeys = array(
        'uid',
        'id'
    );
    $strkeys = array(
        'nickname',
        'title'
    );
    $results = getparam($intkeys, $strkeys);
    $mpurl .= '&' . implode('&', $results ['urls']);

    // 构建api请求url与参数数组
    $param = array(
        'do' => 'getSquareList',
        'format' => 'json',
        'start' => $start,
        'length' => $perpage,
    );
    $param = array_merge($param, $results ['param']);
    // 读取api数据
    $content = get_api_content($_SC['apiurl'], $param);
    $content = json_decode($content);
    
    if($content->errorNo){
    	//The Admin userinfo is NULL! 
	    $msg = "errorNo:" . $content->errorNo;
	    cpmessage($msg);
    }
    
    $list = object_to_array($content->resultData);
	
    // page
    $pageurl = multi($start, $perpage, $mpurl);
    $nextpurl = $pageurl['next'];
    $prevpurl = $pageurl['prev'];
    $mpurl = $pageurl['urls'];
    $firstpurl = $pageurl['first'];

    $perpages = array(
        $perpage => ' selected'
    );
}






